package com.bdrm.controller.user;

import com.bdrm.context.BaseContext;
import com.bdrm.entity.AddressBook;
import com.bdrm.result.Result;
import com.bdrm.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址薄相关接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 查询当前用户的所有地址
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询当前用户的所有地址")
    public Result<List<AddressBook>> list(){
        AddressBook addressBook = new AddressBook();
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        List<AddressBook> list=addressBookService.list(addressBook);
        return Result.success(list);

    }

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result addAddressBook(@RequestBody AddressBook addressBook) {
        log.info("新增地址:{}", addressBook);
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getById(@PathVariable Long id){
        AddressBook addressBook =addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 根据id修改数据
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改数据")
    public Result update(@RequestBody AddressBook addressBook){
        log.info("修改地址:{}", addressBook);
        addressBookService.update(addressBook);
        return Result.success();
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址:{}", addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除地址")
    public Result deleteById(@PathVariable Long id){
        log.info("删除地址:{}", id);
        addressBookService.deletById(id);
        return Result.success();
    }

    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault(){
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());
       //查询相等的userId和默认的地址为1的
        List<AddressBook> list=addressBookService.list(addressBook);
        if (list.size()==1 || list != null) {
            return Result.success(list.get(0));
        }
        return Result.error("没有查询到默认地址");
    }
}

